;;;; Copyright 2009 Peter von Etter

;;;; This file is part of Autoproject

;;;; Autoproject is free software: you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation, either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; Autoproject is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with Autoproject.  If not, see
;;;; <http://www.gnu.org/licenses/>.

(in-package :autoproject.crud)

(defclass db-adapter () ())

(defgeneric call-with-db (db-adapter fn) 
  (:documentation "
Connects to the database of DB-ADAPTER and executes FN.
DB-ADAPTER may keep connections alive and reuse them."))

(defgeneric call-with-transaction (db-adapter fn)
  (:documentation "
Executes FN in a transaction."))

(defgeneric db-query (db-adapter query)
  (:documentation "
Executes query in the database of DB-ADAPTER.  Needs to be wrapped
with CALL-WITH-DB."))

(defgeneric db-execute (db-adapter statement)
  (:documentation "
Executes statement in the database of DB-ADAPTER.  Needs to be wrapped
with CALL-WITH-DB."))

;; Optional
(defgeneric get-last-id (db-adapter)
  (:documentation "
Returns the id of the last inserted row."))

;; Default implementation.
(defmethod get-last-id (db-adapter)
  nil)

;; Optional
(defgeneric read-database-tables (db-adapter)
  (:documentation "
Inspects the database of DB-ADAPTER and returns two values: a list of
tables in the database, and a list of lists specifying the fields in
each table.

Example return values:

  (\"foo\" \"bar\")

  ((\"id\" \"name\" \"date\")
   (\"id\" \"foo_id\" \"blargh\"))

This would mean that the table \"foo\" has the fields (\"id\" \"name\"
\"date\") while table \"bar\" has the fields (\"id\" \"foo_id\"
\"blargh\")"))
